package com.example.shushiyunsst.fragment.main.mine.child

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.glidmodule.GlideApp
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.constant.CURRENT_DEVICE_TOKEN
import com.example.shushiyunsst.fragment.main.mine.child.bean.PayingWayListBean
import com.example.shushiyunsst.fragment.main.wallet.bean.MarketBean
import com.example.shushiyunsst.util.ADD_PAYING_WAY
import com.example.shushiyunsst.util.PAYING_WAY_LIST
import com.example.shushiyunsst.util.startNavigation
import com.example.wallet.db.UserInfoManager
import com.example.wallet.utils.Preference
import com.google.android.material.button.MaterialButton
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.ImageViewerPopupView
import com.lxj.xpopup.interfaces.XPopupImageLoader
import ikidou.reflect.TypeBuilder
import java.io.File
import java.lang.reflect.Type

/**
 *收款信息
 * */
@Route(path = PAYING_WAY_LIST)
class PayingWayListFragment : SimpleListFragment<PayingWayListBean>(), OnItemChildClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isEnableLoadMore=false
        isAddDividerItemDecoration=false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        addHeader(R.layout.comment_header_layout)
        val mToolbar: Toolbar = mHeader.findViewById(R.id.mToolbar)
        mToolbar.title = "收款信息"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
        mFooter.removeAllViews()
        addFooter(R.layout.paying_way_list_fragment_footer)
        val interAdd: MaterialButton = mFooter.findViewById(R.id.interAdd)
        interAdd.setOnClickListener {
            ARouter.getInstance().build(ADD_PAYING_WAY).startNavigation(this)
        }
        mAdapter?.addChildClickViewIds(R.id.aliPayCode,R.id.wxCode)
        mAdapter?.setOnItemChildClickListener(this)
    }

    override fun getItemLayout(): Int {
        return R.layout.paying_way_list_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: PayingWayListBean?) {
        helper?.setText(R.id.wxAccount,item?.wxAccount)
            ?.setText(R.id.wxRelName,item?.realName)
            ?.setText(R.id.aliPayAccount,item?.alipayAccount)
            ?.setText(R.id.aliPayRelName,item?.realName)
            ?.setText(R.id.bankAccount,item?.bankNo)
            ?.setText(R.id.bankRelName,item?.realName)
            ?.setText(R.id.bankName,item?.bankName)
            ?.setText(R.id.bankChildName,item?.bankNameChild)
            ?.setImageNetUrl(R.id.aliPayCode,item?.alipayUrl)
            ?.setImageNetUrl(R.id.wxCode,item?.wxUrl)
            ?.setViewVisible(R.id.wxLayout,item?.showType==PayingWayListBean.WEI_CHAT_PAY)
            ?.setViewVisible(R.id.aliPayLayout,item?.showType==PayingWayListBean.A_LI_PAY)
            ?.setViewVisible(R.id.bankLayout,item?.showType==PayingWayListBean.BANK_CARD_PAY)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(PayingWayListBean::class.java)
            .endSubType().build()
    }

    override fun netRequest() {
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val deviceTokens = Preference.HOLDER.getCustomAppProfile(CURRENT_DEVICE_TOKEN)
        val map = mapOf(
            "deviceToken" to deviceTokens,
            "uid" to userInfo?.uid
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryMyPayment)
            .success {
                parseDate(it)
            }.error { _, _ ->
                onFailed()
            }.failure { onFailed() }
            .build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val payment = obj["payment"] ?: ""
        val ls= mutableListOf<PayingWayListBean>()
        val list = JSON.parseObject(JSON.toJSONString(payment), PayingWayListBean::class.java)
        if (list != null) {

            if (list.isSupportWx==1){
                val mWx = JSON.parseObject(JSON.toJSONString(payment), PayingWayListBean::class.java)
                mWx.showType=PayingWayListBean.WEI_CHAT_PAY
                ls.add(mWx)
            }

           if (list.isSupportAipay==1){
               val aliAay = JSON.parseObject(JSON.toJSONString(payment), PayingWayListBean::class.java)
               aliAay.showType=PayingWayListBean.A_LI_PAY
               ls.add(aliAay)
           }



            if (list.isSupportBank==1){
                val mBank = JSON.parseObject(JSON.toJSONString(payment), PayingWayListBean::class.java)
                mBank.showType=PayingWayListBean.BANK_CARD_PAY
                ls.add(mBank)
            }
        }
        loadListDate(ls)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = mAdapter.getItem(position)

        when(view.id){
            R.id.aliPayCode->{
                val imageViewerPopupView = ImageViewerPopupView(_mActivity)
                imageViewerPopupView.setImageUrls(listOf(item?.alipayUrl))
                imageViewerPopupView.setXPopupImageLoader(object : XPopupImageLoader {
                    override fun loadImage(position: Int, uri: Any, imageView: ImageView) {
                        GlideApp.with(this@PayingWayListFragment)
                            .load(uri)
                            .centerCrop()
                            .into(imageView)
                    }

                    override fun getImageFile(context: Context, uri: Any): File {
                        return File(uri.toString())
                    }
                })
                XPopup.Builder(_mActivity)
                    .asCustom(imageViewerPopupView)
                    .show()
            }
            R.id.wxCode->{
                val imageViewerPopupView = ImageViewerPopupView(_mActivity)
                imageViewerPopupView.setImageUrls(listOf(item?.wxUrl))
                imageViewerPopupView.setXPopupImageLoader(object : XPopupImageLoader {
                    override fun loadImage(position: Int, uri: Any, imageView: ImageView) {
                        GlideApp.with(this@PayingWayListFragment)
                            .load(uri)
                            .centerCrop()
                            .into(imageView)
                    }

                    override fun getImageFile(context: Context, uri: Any): File {
                        return File(uri.toString())
                    }
                })
                XPopup.Builder(_mActivity)
                    .asCustom(imageViewerPopupView)
                    .show()
            }
        }
    }
}